#include <dbg.h>

#include <cassert>

using namespace std;

class Solution {
public:
    int numFriendRequests(vector<int>& ages) {
        vector<int> cnt(max_age, 0);
        for (int age : ages) {
            ++cnt[age];
        }

        int res = 0;

        int cntWindow = 0;

        int ageY = 0;
        for (int ageX = 0; ageX < max_age; ++ageX) {
            cntWindow += cnt[ageX];
            if (ageY * 2 <= ageX + 14) {  // 不能发送好友请求
                cntWindow -= cnt[ageY];
                ++ageY;
            }
            if (cntWindow > 0) {  // 存在可以发送好友请求的用户
                res += cnt[ageX] * cntWindow - cnt[ageX];
            }
        }
        return res;
    }

private:
    const int max_age = 121;
};

int main() {
    Solution s;

    vector<int> ages1{16, 16};
    vector<int> ages2{16, 17, 18};
    vector<int> ages3{20, 30, 100, 110, 120};

    assert(s.numFriendRequests(ages1) == 2);
    assert(s.numFriendRequests(ages2) == 2);  // 17 -> 16 ，18 -> 17
    assert(s.numFriendRequests(ages3) ==
           3);  // 110 -> 100 ，120 -> 110 ，120 -> 100

    return 0;
}